package com.example.office.util.pubTopic;

import org.zeromq.ZMQ;

/**
 * @ Author     ：jujh
 * @ Date       ：Created in 14:38 2018/12/24
 * @ Description：订阅
 */
public class ZMQ_SUB {

    public static void main(String[] args) {
        //准备上下文
        //套接字连接至服务器
        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
        subscriber.connect("tcp://localhost:5555");
        //订阅任何主题（An empty 'option_value' of length zero shall subscribe to all incoming messages）
        subscriber.subscribe("".getBytes());
        for (int i = 0; i < 100; i++) {
            //Receive a message.
            String string = new String(subscriber.recv(0));
            System.out.println("recv1:" + string);
        }
        //关闭套接字和上下文
        subscriber.close();
        context.term();
    }
}
